/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.architexa.org.eclipse.draw2d;

import com.architexa.org.eclipse.draw2d.geometry.Dimension;
import com.architexa.org.eclipse.draw2d.geometry.Point;

/**
 * RelativeBendpoint is a Bendpoint that calculates its location based on its distance
 * from the start and end points of the {@link Connection}, as well as its weight. See
 * {@link #setWeight(float)} for a description of what behavior different weights will
 * provide.
 */
public class RelativeBendpoint
	implements Bendpoint
{

private Connection connection;
private float weight = 0.5f;
private Dimension d1, d2;

/**
 * Constructs a new RelativeBendpoint.
 * 
 * @since 2.0
 */
public RelativeBendpoint() { }

/**
 * Constructs a new RelativeBendpoint and associates it with the given Connection.
 * @param conn The Connection this Bendpoint is associated with
 * @since 2.0
 */
public RelativeBendpoint(Connection conn) {
	setConnection(conn);
}

/**
 * Returns the Connection this Bendpoint is associated with.
 * @return The Connection this Bendpoint is associated with
 * @since 2.0
 */
protected Connection getConnection() {
	return connection;
}

/**
 * Calculates and returns this Bendpoint's new location.
 * @return This Bendpoint's new location
 * @since 2.0
 */
public Point getLocation() {
	Point a1 = getConnection().getSourceAnchor().getReferencePoint();
	Point a2 = getConnection().getTargetAnchor().getReferencePoint();
	
	Point p = new Point();
	Dimension dim1 = d1.getCopy(), dim2 = d2.getCopy();
	
	getConnection().translateToAbsolute(dim1);
	getConnection().translateToAbsolute(dim2);
	
	p.x = (int)((a1.x + dim1.width) * (1f - weight) + weight * (a2.x + dim2.width));
	p.y = (int)((a1.y + dim1.height) * (1f - weight) + weight * (a2.y + dim2.height));
	getConnection().translateToRelative(p);
	return p;
}

/**
 * Sets the Connection this bendpoint should be associated with.
 * @param conn The Connection this bendpoint should be associated with
 * @since 2.0
 */
public void setConnection(Connection conn) {
	connection = conn;
}

/**
 * Sets the Dimensions representing the X and Y distances this Bendpoint is from the start
 * and end points of the Connection. These Dimensions are generally set once and are used
 * in  calculating the Bendpoint's location.
 * @param dim1 The X and Y distances this Bendpoint is from the start of the Connection
 * @param dim2 The X and Y distances this Bendpoint is from the end of the Connection
 * @since 2.0
 */
public void setRelativeDimensions(Dimension dim1, Dimension dim2) {
	d1 = dim1;
	d2 = dim2;
}

/**
 * Sets the weight this Bendpoint should use to calculate its location. The weight should
 * be between 0.0 and 1.0. A weight of 0.0 will cause the Bendpoint to follow the start
 * point, while a weight of 1.0 will cause the Bendpoint to follow the end point. A weight
 * of 0.5 (the default) will cause the Bendpoint to maintain its original aspect ratio
 * between the start and end points.
 * @param w The weight
 * @since 2.0
 */
public void setWeight(float w) {
	weight = w;
}

}
